package erfgame.frontend.desktop.view.world.create;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.util.List;
import java.util.ResourceBundle;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import org.jdesktop.beansbinding.Converter;
import org.jdesktop.beansbinding.AutoBinding.UpdateStrategy;
import org.metawidget.swing.SwingMetawidget;
import org.metawidget.swing.widgetprocessor.binding.beansbinding.BeansBindingProcessor;
import org.metawidget.swing.widgetprocessor.binding.beansbinding.BeansBindingProcessorConfig;

import erfgame.core.world.Frequency;
import erfgame.core.world.Humidity;
import erfgame.core.world.LongitudinalZone;
import erfgame.core.world.TerrainMaterialTypeWeight;
import erfgame.core.world.WorldGenerationParameters;

import erfgame.frontend.desktop.view.AbstractView;


public class CreateWorldView extends AbstractView {

	public static final int ACTION_TYPE_CANCEL = 0;
	public static final int ACTION_TYPE_CREATE = 1;
	
	private static final String RESOURCES = "bundles.CreateWorldView";
	
	private JPanel component;
	private JButton cancelButton;
	private JButton createButton;
	
	public CreateWorldView() {
		
	}
	
	public void init( WorldGenerationParameters parameters ) {
		
		ResourceBundle resources = ResourceBundle.getBundle( RESOURCES );
		
		this.component = new JPanel( new GridBagLayout() );
		
		this.cancelButton = new JButton( resources.getString( "cancel" ) );
		this.createButton = new JButton( resources.getString( "create" ) );

		GridBagConstraints panelConstraints = new GridBagConstraints();
		panelConstraints.anchor = GridBagConstraints.NORTH;
		panelConstraints.gridwidth = GridBagConstraints.REMAINDER;
		panelConstraints.fill = GridBagConstraints.HORIZONTAL;
		panelConstraints.weightx = 1.0;
		panelConstraints.weighty = 1.0;
		panelConstraints.insets = new Insets(10, 10, 0, 10);
		
		SwingMetawidget metawidget = new SwingMetawidget();
		metawidget.setConfig("metawidget/create_world_view.xml");
		BeansBindingProcessor.registerConverter(Humidity.class, String.class, new Converter<Humidity, String>() {

			@Override
			public String convertForward(Humidity h) {
				return h.getDisplayName();
			}

			@Override
			public Humidity convertReverse(String s) {
				return Humidity.valueOf(s);
			}
			
		});
		BeansBindingProcessor.registerConverter(Frequency.class, String.class, new Converter<Frequency, String>() {

			@Override
			public String convertForward(Frequency h) {
				return h.getDisplayName();
			}

			@Override
			public Frequency convertReverse(String s) {
				return Frequency.valueOf(s);
			}
			
		});
		BeansBindingProcessor.registerConverter(LongitudinalZone.class, String.class, new Converter<LongitudinalZone, String>() {

			@Override
			public String convertForward(LongitudinalZone h) {
				return h.getDisplayName();
			}

			@Override
			public LongitudinalZone convertReverse(String s) {
				return LongitudinalZone.valueOf(s);
			}
		});
		metawidget.addWidgetProcessor( 
				new BeansBindingProcessor(
						new BeansBindingProcessorConfig().setUpdateStrategy( UpdateStrategy.READ_WRITE )
				) 
		);
//		metawidget.addWidgetProcessor(
//				new BeanUtilsBindingProcessor(
//					new BeanUtilsBindingProcessorConfig().setPropertyStyle( BeanUtilsBindingProcessorConfig.PROPERTYSTYLE_JAVABEAN )
//				)
//		);		
		metawidget.setToInspect( parameters );	
		metawidget.add( createElementsTable(parameters) );
		
		this.component.add( metawidget, panelConstraints );
		
		GridBagConstraints buttonConstraints = new GridBagConstraints();
		buttonConstraints.anchor = GridBagConstraints.SOUTH;
		buttonConstraints.fill = GridBagConstraints.HORIZONTAL;
		buttonConstraints.weightx = 1.0;
		buttonConstraints.weighty = 0.0;
		buttonConstraints.insets = new Insets( 0, 10, 10, 10 );
	
		JPanel buttonPanel = new JPanel( new BorderLayout() );
		buttonPanel.add( this.createButton, BorderLayout.WEST );
		buttonPanel.add( this.cancelButton, BorderLayout.EAST );
		this.component.add( buttonPanel, buttonConstraints );
		
		this.cancelButton.addActionListener( 
				this.createFireViewActionListener( ACTION_TYPE_CANCEL ) 
		);
		this.createButton.addActionListener( 
				this.createFireViewActionListener( ACTION_TYPE_CREATE ) 
		);
	}
	
	public void destroy() {
		this.component = null;
		this.cancelButton = null;
		this.createButton = null;
	}
	
	public JComponent getComponent() {
		return this.component;
	}

	private Component createElementsTable( WorldGenerationParameters parameters ) {
		// TODO : introspecting table model
		DefaultTableModel model = new DefaultTableModel( new Object[]{"Material","Weight"}, 0);
		List<TerrainMaterialTypeWeight> weights = parameters.getTerrainTypeWeights();
		for( TerrainMaterialTypeWeight weight : weights ) {
			model.addRow( new Object[]{ weight.getMaterialName(), weight.getWeight() } );
		}
		JTable table = new JTable( model );
		table.setCellSelectionEnabled(false);
		table.setEnabled(false);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setName("terrainTypeWeights");
		return scrollPane;
	}
}
